<?php

class Date
{

    /**
     * 格式化时间
     * @param int $time 可选参数，默认为当前时间戳
     * @return string
     */
    public static function formatTime(int $time = 0)
    {
        self::setTimeZone();
        if ($time <= 0) {
            return date("Y-m-d H:i:s", time());
        } else {
            return date("Y-m-d H:i:s", $time);
        }
    }

    /**
     * 获取当前时间戳
     * @return int
     */
    public static function now(): int
    {
        self::setTimeZone();
        return time();
    }

    /**
     * 如果是当天的那么只返回时间，如果是昨天的那么返回'昨天',如果是本周的返回星期几，如果是上周的显示年月日
     * @param int $time
     * @return string
     */
    public static function timeInfo(int $time):string
    {
        self::setTimeZone();
        $now = time();
        $diffDay = self::diffDay($now, $time);
        $isSameWeek = self::isSameWeek($now, $time);

        if ($diffDay === 1){
            return "昨天";
        }

        // 如果是同一周的, 那么只返回星期几
        if ($isSameWeek){
            return "星期" . self::getWeek($time);
        }else{
            $diffDay = self::diffDay(self::getMonday($now), $time);

            // 如果是上周的那么返回上周几
            if ($diffDay <= 7){
                return "上周" . self::getWeek($time);
            }else{
                return self::getDate($time);
            }
        }
    }

    /**
     * 获取星期几
     * @param int $time 时间戳
     * @return string
     */
    public static function getWeek(int $time): string
    {
        $weekArray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组
        return $weekArray[date("w", $time)];
    }

    /**
     * 判断两个时间是否是同一周
     * @param int $time1
     * @param int $time2
     * @return bool
     */
    public static function isSameWeek(int $time1, int $time2): bool
    {
        $monday1 = self::getMonday($time1);
        $monday2 = self::getMonday($time2);
        return $monday1 == $monday2;
    }


    /**
     * 获取日期
     * @param int $time
     * @return string
     */
    public static function getDate(int $time): string
    {
        return date("Y/m/d", $time);
    }

    /**
     * 获取时间
     * @param int $time
     * @return string
     */
    public static function getTime(int $time): string
    {
        return date("H:i", $time);
    }

    /**
     * 获取指定日期的星期一
     * @param int $time
     * @return int
     */
    public static function getMonday(int $time): int
    {
        $week = date("w", $time);
        if ($week == 0) {
            $week = 7;
        }
        return $time - ($week - 1) * 86400;
    }

    /**
     * 获取指定日期的星期日
     * @param int $time
     * @return int
     */
    public static function getSunday(int $time): int
    {
        $week = date("w", $time);
        if ($week == 0) {
            $week = 7;
        }
        return $time + (7 - $week) * 86400;
    }

    /**
     * 计算两个时间的差值天数
     * @param int $startTime 起始时间
     * @param int $endTime 结束时间
     * @return int
     */
    public static function diffDay(int $startTime, int $endTime): int
    {
        return floor(($startTime - $endTime) / 86400);
    }

    /**
     * 设置时区
     * @param string $timeZone
     * @return void
     */
    public static function setTimeZone(string $timeZone = 'PRC')
    {
        if (date_default_timezone_get() === $timeZone) {
            return;
        }
        date_default_timezone_set($timeZone);
    }
}